using System;
using System.Collections.Generic;
using Mahjong.Logic;
using UnityEditor;

namespace Mahjong.Logic
{
public class Data
{
    private Dictionary<int, List<int>> playerHandCards = new Dictionary<int, List<int>>();
    private Dictionary<int, List<int>> playerDiscardCards = new Dictionary<int, List<int>>();
    private Dictionary<int, int> finalScores = new Dictionary<int, int>();
    private Dictionary<int, int> playerLastDrawnCard = new Dictionary<int, int>();//记录某位玩家最后摸到的牌
    
    private Dictionary<int, List<int>> pengCard = new Dictionary<int, List<int>>();//记录碰牌
    private Dictionary<int, List<int>> gangCard = new Dictionary<int, List<int>>();//记录杠牌
    private Dictionary<int, List<int>> chiCard = new Dictionary<int, List<int>>();//记录吃牌
    private Dictionary<int,  string> playerInformations = new Dictionary<int,string>();
    private int winningCard;//记录赢牌
    private int winningPlayer; // 记录赢牌玩家
    private List<int> cardsForDistribute; // 存储发牌队列的字段
    private int playerCount = 4;/*玩家数，后续需要从玩家->manager中读取，暂时设成定值*/
    private int initialCardsPerPlayer = 16;//每位玩家初始手牌数
    private List<int> drawCard;//摸牌区间
    private int goldCard; // 记录金牌  
    private int drawIndex; // 当前摸牌索引
     private int lastDrawnCard; // 最后一张摸到的牌
    
    public enum GangTypeEnum
    {
        MingGang,
        AnGang,
        JiaGang,
    }

    
    //生成所有牌,作为原始牌堆
    public List<int> GenerateAllCards()
    {
        List<int> cards = new List<int>();
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                cards.Add(i); // 万
                cards.Add(i + 10); // 条
                cards.Add(i + 20); // 筒
            }
        }
        for (int j = 0; j < 4; j++)
        {
            cards.Add(31); // 白板
        }
        return cards;
    }

    //提供接口输出玩家数量
    public int GetPlayerCount()
    {
        return playerCount;
    }

    // 记录金牌
   public void SetGoldCard(int card)

    {
        goldCard = card;
    }
    //提供接口给客户端获取金牌数据
    public int GetGoldCard()
    { 
        /* 该值后续需要添加到展示区，用于告知用户金牌是哪张牌*/
       return goldCard;
    }

    
    //设置存储玩家信息
    public void SetPlayerInformation(int playerId, Dictionary<int, string> playerInfo)
    {
        foreach (var info in playerInfo)
        {
            playerInformations[info.Key] = info.Value;
        }
    }
    //提供接口用于获取指定玩家的信息
    public string GetPlayerInformation(int playerId)
    {
        
        if (playerInformations.ContainsKey(playerId))
        {
            return playerInformations[playerId];
        }
        return null;
    }

    //记录移除金后的新牌堆
    public void SetCardsForDistribute(List<int> cards)
    {
        cardsForDistribute = new List<int>(cards);
    }
    //提供接口给客户端获取牌堆
    public List<int> GetCardsForDistribute()
    {
        return new List<int>(cardsForDistribute);
    }

     // 获取当前摸牌索引
    public int GetDrawIndex()
    {
        return drawIndex;
    }

    // 设置当前摸牌索引
    public void SetDrawIndex(int drawIndex)
    {
        this.drawIndex = drawIndex;
    }

    // 记录摸牌区间
    public void SetDrawCard(List<int> drawCard)
    {
         this.drawCard = drawCard; // 创建一个新的列表并赋值给成员变量
    } 
    //提供接口给客户端获取摸牌区间
    public List<int> GetDrawCard()
    {
        return drawCard; 
    }

      //更新最后一张摸到的牌
    public void UpdateLastDrawnCard(int card)
    {
        lastDrawnCard = card;
    }

    // 获取最后一张摸到的牌
    public int GetLastDrawnCard()
    {
        return lastDrawnCard;
    }
        
    //更新弃牌数据
    public void UpdateDiscard(int playerId, int card)
    {
        // 如果该玩家非第一次弃牌，则添加牌到弃牌列表
        if (playerDiscardCards.ContainsKey(playerId))
        {
            playerDiscardCards[playerId].Add(card);
        }
        // 如果该玩家第一次弃牌，则创建一个新的列表并添加牌
        else
        {
            playerDiscardCards[playerId] = new List<int> { card };
        }
    }

    //提供接口给客户端获取最新弃牌数据
    public List<int> GetDiscardCards(int playerId)
    {
        return playerDiscardCards[playerId];
    }
    


    // //接受List<int>作为参数，更新手牌数据
    // public void UpdateHandCard(int playerId, List<int> handCards)
    // {
    //     if (playerHandCards.ContainsKey(playerId))
    //     {
    //         playerHandCards[playerId]=handCards;
    //     }
    //      else
    //     {   
    //         //初始化并使用AddRange方法添加手牌
    //         playerHandCards[playerId] = new List<int>();
    //         playerHandCards[playerId].AddRange(handCards);
    //     }
    // }
    // //接受int作为参数，更新手牌数据  
    // public void UpdateHandCard(int playerId, int card)
    // {
    //     if (playerHandCards.ContainsKey(playerId))
    //     {
    //         playerHandCards[playerId].Add(card);
    //     }
    //     else
    //     {
    //         // 初始化并添加单个手牌
    //         playerHandCards[playerId] = new List<int> { card };
    //     }
    // }
    public void UpdateHandCard(int playerId, List<int> handCards = null, int card = -1)
{
    if (handCards != null)
    {
        // 处理List<int>参数
        if (playerHandCards.ContainsKey(playerId))
        {
            playerHandCards[playerId] = handCards;
        }
        else
        {
            // 初始化并使用AddRange方法添加手牌
            playerHandCards[playerId] = new List<int>();
            playerHandCards[playerId].AddRange(handCards);
        }
    }
    else if (card != -1)
    {
        // 处理int参数
        if (playerHandCards.ContainsKey(playerId))
        {
            playerHandCards[playerId].Add(card);
        }
        else
        {
            // 初始化并添加单个手牌
            playerHandCards[playerId] = new List<int> { card };
        }
    }
}
    //提供接口给客户端获取最新手牌数据
        public List<int> GetHandCards(int playerId)
    {
        return playerHandCards[playerId];
    }
    
    // 记录最后摸到的牌
    public int GetLastDrawnCard(int playerId)
    {
        //out int是输出参数，在内部赋值并传递回来
        //TryGetValue方法是字典类的方法，用来判断字典中是否有指定的键值对，如果有则返回true，并将值赋给out参数，如果没有则返回false，并将out参数的值设为默认值
        if (lastDrawnCard.TryGetValue(playerId, out int card))
        {
            return card;
        }
    // 如果还没有摸过牌，返回-1
    return -1; 
    }
    

    //记录胡的那张牌的信息和胡牌人的id
    /*可以直接用最后一张摸起的牌吗*/
    public void RecordWinningCard(int playerId, int card)
    {
        winningCard = card;
        winningPlayer = playerId;
    }
    //提供接口给客户端获取胡牌人id数据
    public int GetWinningPlayer()
    {
        return winningPlayer;
    }

    //记录碰牌
    public void RecordPeng(int playerId, int card)
    {
        if(!pengCard.ContainsKey(playerId))
        {
            pengCard[playerId]=new List<int>{card};
        }
        pengCard[playerId].AddRange(new[]{card,card,card});
    }

    //提供获取碰牌的接口
    public List<int> GetPengCards(int playerId)
    {
        return pengCard.ContainsKey(playerId)?pengCard[playerId]:new List<int>();
    }

    // 更新碰牌数据
    public void UpdatePengCards(int playerId, List<int> pengCards)
    {
        if (pengCard.ContainsKey(playerId))
        {
            pengCard[playerId] = pengCards;
        }
        else
        {
            pengCard.Add(playerId, pengCards);
        }
    }

    
    //记录杠牌信息
    public void RecordGang(int playerId, int card, GangTypeEnum gangType)
    {
        if (!gangCard.ContainsKey(playerId))
        {
            gangCard[playerId] = new List<int>();
        }
        gangCard[playerId].Add(card);
        // 可以根据gangType添加更多的记录逻辑，比如区分不同类型的杠牌
    }
    //提供获取杠牌的接口
    public List<int> GetGangCards(int playerId)
    {
        return gangCard.ContainsKey(playerId)?gangCard[playerId]:new List<int>();
    }

    //更新杠牌数据
    public void UpdateGangCards(int playerId, List<int> gangCards)
    {
        if (gangCard.ContainsKey(playerId))
        {
            gangCard[playerId] = gangCards;
        }
        else
        {
            gangCard.Add(playerId, gangCards);
        }
    }
    //记录吃牌信息
    public void RecordChi(int playerId, int card,List <int> chiCards)
    {
        
    }
    
    //判断游戏是否结束
    public bool IsGameEndConditionMet()
    {
        //出现赢家
        if (winningPlayer != 0)
        {
            return true;
        }
        
        //牌打完
        if (drawCard == null || drawCard.Count == 0)
        {
            return true;
        }
        return false;
    }

    public void CalculateFinalScores()
    {
        // 计算最终得分
    }

    //重置游戏数据
    public void ResetGameData()
    {
        playerHandCards.Clear();
        playerDiscardCards.Clear();
        finalScores.Clear();
        pengCard.Clear();
        gangCard.Clear();
        chiCard.Clear();
        winningCard = 0;
        winningPlayer = 0;
        cardsForDistribute = new List<int>();
        drawCard = new List<int>();
        lastDrawnCard.Clear();// 清空上一个摸到的牌
    }

    public Dictionary<int, int> GetFinalScores()
    {
        return finalScores;
    }
}
}